package chain_user_config

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/services/chain_user_config"
	"net/http"
)

type deleteRequest struct {
	Id string `uri:"id"`
}

type deleteResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func (h *handler) Delete() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(deleteRequest)
		res := new(deleteResponse)

		if err := ctx.ShouldBindURI(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		deleteChainData := new(chain_user_config.DeleteChainData)
		deleteChainData.Id = req.Id
		err := h.chainUserConfigService.Delete(ctx, deleteChainData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.DeleteProductError,
				code.Text(code.DeleteProductError)).WithError(err),
			)
			return
		}
		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
